package com.handinfo.engine;


import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.ConnectionNotFoundException;

import com.handinfo.engine.net.HttpConnect;
import com.handinfo.game.GameScreen;
import com.handinfo.util.KillKeShowMem;
import com.handinfo.util.Tools;

/**
 * 程序入口类
 *
 * 该类是程序的控制类,以及程序消息处理 作用：控制程序的启动，暂停，关闭
 *
 * @author Xia Mingwei
 * @version 1.0
 */
public class GameMIDlet extends MIDlet
{

	public static GameMIDlet s_instance;
	public static GameManager s_manager;
	public static AbstractGameScreen s_canvas;
	public static boolean s_isJumpWap = false;
	public static String s_JumpUrl = null;
	public KillKeShowMem m_kill;
	public HttpConnect m_http;//HTTP对象
	public static final String m_iprecord = "ipaddress";//推荐IP地址存档
	public static boolean isHaveIP = false;
	public static final String MORE_GAME_URL = "http://game.10086.cn/a/";

	public GameMIDlet()
	{
//		 m_kill = new KillKeShowMem();
//		 m_kill.kill();
		s_instance = this;
		s_canvas = new GameScreen();
		GameManager.createGameManager(s_canvas);
	}

	public void startApp() throws MIDletStateChangeException
	{
		if (Display.getDisplay(this).getCurrent() == null) {
			Display.getDisplay(this).setCurrent(s_canvas);
			// 在这里启动游戏主线程
			GameManager.getInstance().startThread();

			// 判断是否有IP地址存档 没有请求HTTP
//			if(!Tools.haveRecord(m_iprecord)){
//				/** 获取服务器列表*/
//				 StringBuffer buff = new StringBuffer("DTgame_billing/http_findServerInfo?pcOrPh=1");
//				 m_http = new HttpConnect(buff.toString(), "211.151.33.69:8080", false);
//				 m_http.SetState(m_http.CM_GETSERVERLIST);
//				 new Thread(m_http).start();
//				 buff = null;
//			}else{
				//loadAddress();
				isHaveIP = true;
//			}
		}
		else if (Display.getDisplay(this).getCurrent() == s_canvas) {
			// moto部分机器在挂起恢复后不会调用shownotify而是调用startapp
			s_canvas.showNotify();
		}
	}


	public void pauseApp()
	{
		if (s_canvas != null)
			s_canvas.hideNotify();
	}

	public void destroyApp(boolean unconditional)
	{
		Display.getDisplay(this).setCurrent(null);
		if (s_isJumpWap) {
			try {
				Tools.debugPrintln("打开URL : " + s_JumpUrl);
				if (s_JumpUrl != null) {
					GameMIDlet.s_instance.platformRequest(s_JumpUrl);
				}
			}
			catch (ConnectionNotFoundException ex) {
				Tools.debug(ex);
			}
			finally {
			}
		}
		s_canvas = null;
		notifyDestroyed();
	}

	public static final void loadAddress()
	{
//		byte[] data = Tools.loadRecord(m_iprecord, 1);
//		try {
//			if (data == null) {
//				throw new Exception();
//			}
//			else {
//				ByteArrayInputStream bais = new ByteArrayInputStream(data);
//				DataInputStream dis = new DataInputStream(bais);
//				GameManager.SERVER_URL = dis.readUTF();
//				GameManager.SERVER_PORT = dis.readInt();
//				bais.close();
//				dis.close();
//				bais = null;
//				dis = null;
//			}
//		}
//		catch (Exception ex) {
//			ex.printStackTrace();
//		}
	}

    @Override
    public void onBackPressed() {

    }

	@Override
	public void onPauseSound() {
		Music.getInstance().pauseBackground();
	}

	@Override
	public void onResumeSound() {
		Music.getInstance().playBackground();
	}
}
